package com.voltmao.controller.module.sys;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.voltmao.persistent.model.SysDictionary;
import com.voltmao.service.application.sys.DictionaryServiceImpl;
import com.voltmao.webpro.frame.analyze.DTOSmith;
import com.voltmao.webpro.frame.dto.ApiResult;
import com.voltmao.webpro.frame.dto.status.Constants;
import com.voltmao.webpro.utils.StringUtils;
import com.voltmao.webpro.utils.http.HttpRequestUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 字典控制层
 *
 * @author zhanglin
 * @date 2018/04/13
 */

@Controller
@RequestMapping(value = "/module/sys/dictionary")
public class DictionaryController {

    @Autowired
    DictionaryServiceImpl dictionaryService;
    @ResponseBody
    @RequestMapping(value = "/apiDictionary.json", method = RequestMethod.GET)
    public ApiResult getDictionary(HttpServletRequest request){
        /*
         * 根据pcode拿到所有的记录
         */
        return new DTOSmith(request).builderApiResult((request1, apiResult)->{
            Map<String, Object> params = HttpRequestUtil.getParameterMap(request1);
            String pcode = params.get("pcode")+"";
            List<SysDictionary> lists = dictionaryService.getDictionaryByPCode(pcode);
            if(StringUtils.isNotEmpty(lists) && lists.size() > 0){
                apiResult.setStatus(Constants.SUCCESS);
            } else{
                apiResult.setStatus(Constants.DATA_NULL);
            }
            return lists;
        });

    }
}
